<?php

require_once ('config.php');
require_once ('includes/inc.mysql.php');

require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php');

$conn = new mysli();

$q = 'SELECT AVG( temp ) as temp , MINUTE( '. MYSQL_TABLE .'.time ) as time FROM '. MYSQL_TABLE .' GROUP BY YEAR( '. MYSQL_TABLE .'.time ), MONTH( '. MYSQL_TABLE .'.time ), DATE( '. MYSQL_TABLE .'.time ), HOUR( '. MYSQL_TABLE .'.time ), FLOOR( MINUTE( '. MYSQL_TABLE .'.time ) / 5 ) ORDER BY '. MYSQL_TABLE .'.time DESC LIMIT 0, 12';
$result = $conn->q($q);

$index = 0;

while($row = mysql_fetch_row($result, MYSQL_ASSOC)) {
	$data[] = $row['temp'];
	$label[] = $row['time'];
}

$data = array_reverse($data);
$label = array_reverse($label);

// The setup
$graph = new Graph(500,250);
$graph->SetScale("textlin",10,30);

$theme_class = new UniversalTheme;


$graph->SetTheme($theme_class);
$graph->img->SetAntiAliasing(false);
$graph->title->Set('Hourly (5 minute average)');
$graph->SetBox(false);

$graph->img->SetAntiAliasing();

$graph->yaxis->HideZeroLabel();
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

$graph->xgrid->Show();
$graph->xgrid->SetLineStyle("solid");
$graph->xaxis->SetTickLabels($label);
$graph->xgrid->SetColor('#E3E3E3');

// Insert data to graph
$p1 = new LinePlot($data);
$graph->Add($p1);
$p1->SetColor("#6495ED");
//$p1->SetLegend('Line 1');


$graph->legend->SetFrameWeight(1);

// Output line
$graph->Stroke();


?>